package com.erpanderp.wam.tm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DualListModel;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.tm.model.entity.Band;
import com.erpanderp.wam.tm.model.entity.RecruitmentRequest;
import com.erpanderp.wam.tm.model.entity.Technology;
import com.erpanderp.wam.tm.model.service.RRFDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class RRFSBean implements Serializable {

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	private WAMUser wamUser;
	
	RRFDAO rrfDAO = new RRFDAO();
	private RecruitmentRequest slectedRecruitmentRequest = new RecruitmentRequest();
	List<RecruitmentRequest> rrfRequests = new ArrayList<RecruitmentRequest>();
	private DataTable rrfTable;
	
	
    
    private DualListModel<String> primarySkills;
	List<String> primarySkillSouce = new ArrayList<String>();
	List<String> primarySkillTarget = new ArrayList<String>();
    
    private List<Band> bands = new ArrayList<Band>();
    private List<Band> businessFunctions = new ArrayList<Band>();
    private List<Band> supportFunctions = new ArrayList<Band>();
    private List<Employee> recruiters = new ArrayList<Employee>();
    private List<Technology> technologies = new ArrayList<Technology>();
    private List<Employee> interviewers = new ArrayList<Employee>();
    
    private Map<String, Long> bandsMap = new HashMap<String, Long>();
    private Map<String, Long> businessFunctionMap = new HashMap<String, Long>();
    private Map<String, Long> supportFunctionMap = new HashMap<String, Long>();
    private Map<String, Long> recruitersMap = new HashMap<String, Long>();
    private Map<String, String> technologiesMap = new HashMap<String, String>();
    private Map<String, Long> interviewersMap = new HashMap<String, Long>();
    private Map<String, Long> interviewers2Map = new HashMap<String, Long>();
    
    

	private Map<String,String> clientMap = CommonLookup.getClient();
    private Map<String,String> interviewLevelMap = CommonLookup.getInterviewLevel();
    private Map<String,String> recruitmentModelMap = CommonLookup.getRecruitmentMode();
	private Map<String,String> recruitmentPriorityMap = CommonLookup.getRecruitmentPriority();
    
    private Long selectedBandId;
    private String selectedBusinessFunction;
    private String selectedSupportFunction;
    private String selectedTechnology;
    private Long selectedRecruiterId;
    private String selectedClient;
    private String selectedInterviewLevel= CommonLookup.SINGLE_LEVEL_VALUE;
    private Long selectedInterviewerOne;
    private Long selectedInterviewerTwo;
    private String selectedRecruitmentMode;
    private String selectedRecruitmentPriority;
    private String recruitmentRequestCode;
    private boolean FORM_VALID;
	
	

	public RRFSBean(){
		setWamUser((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		System.out.println("LOGIN EMPLOYEE ID :" + wamUser.getEmployee().getEmployeeId());		
		
        primarySkillSouce = rrfDAO.getSkillsByTechnology(1l);
		primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget); 
		
		rrfRequests = rrfDAO.getRRFSByEmployeeId(wamUser.getEmployee().getEmployeeId());
		
		slectedRecruitmentRequest.setBand(new Band());
		slectedRecruitmentRequest.setInterviewer1(new Employee());
		slectedRecruitmentRequest.setInterviewer2(new Employee());
		slectedRecruitmentRequest.setEmployee(new Employee());
		slectedRecruitmentRequest.setRecruiter(new Employee());
		slectedRecruitmentRequest.setCenterHead(new Employee());
		
	}
	
	public void viewOrEditActionListener(ActionEvent e){
		System.out.println("Listener...");
		slectedRecruitmentRequest = (RecruitmentRequest) e.getComponent().getAttributes().get("slectedRecruitmentRequest");
		
		//System.out.println("Selected  rrf id : Tostr::::::::::"+slectedRecruitmentRequest.getRecruitmentRequestCode().toString());
		
		System.out.println("Selected  rrf id : ::::::::::"+slectedRecruitmentRequest.getRecruitmentRequestCode());
		System.out.println("BAND: " + slectedRecruitmentRequest.getBand());
		//Get Bands
		bands = rrfDAO.getAllBands();
		for (Band band : bands) {
			bandsMap.put(band.getBand(), band.getBandId());
		}
		
		//Get Business Functions
		businessFunctions = rrfDAO.getAllBands();
		for (Band businessFunction : businessFunctions) {
			businessFunctionMap.put(businessFunction.getBusinessFunction(), businessFunction.getBandId());
		}
		
		//Get Support Functions
		supportFunctions = rrfDAO.getAllBands();
		for (Band band : supportFunctions) {
			supportFunctionMap.put(band.getSupportFunction(), band.getBandId());
		}
		
		//Get Recruiter List
		recruiters=rrfDAO.getAllRecruiters();
		for (Employee recruiter : recruiters) {
			recruitersMap.put(recruiter.getFirstName()+" "+recruiter.getLastName(), recruiter.getEmployeeId());
		}
		
		//Get Technologies list
		
		
		technologies=rrfDAO.getAllTechnologies();
		for (Technology technology : technologies) {
			technologiesMap.put(technology.getDisplayName(), technology.getDisplayName());
		}
		
		//Get Interviewers List
		changeInterviewerListener(null);
		
		//Get Pick List primary Skills	
		SAPERPModule technologyId1= slectedRecruitmentRequest.getTechnology();
        primarySkillSouce = rrfDAO.getSkillsByTechnology(technologyId1.getSAPERPModuleId());
		primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget); 
		
		String addedPrimarySkills =  slectedRecruitmentRequest.getPrimarySkills();
		String pskills[] = addedPrimarySkills.split(",\\s*");		
		for (String item : pskills)
			{
			 primarySkillTarget.add(item);
			}

		//return null;
		
	}
	
 public void changeInterviewerListener(AjaxBehaviorEvent e) {
		
		if (selectedTechnology == null || selectedTechnology == "") {
			System.out.println("No selected Technology==="+selectedTechnology);
		}else{
			
			if(selectedInterviewLevel.equalsIgnoreCase("Single Level")){
				interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
				for (Employee interviewers1List : interviewers) {
					interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());					
				}	
			}
			if(selectedInterviewLevel.equalsIgnoreCase("Two Level")){				
				interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
				for (Employee interviewers1List : interviewers) {
					interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());
				}	
				
				for (Employee interviewers2List : interviewers) {
					interviewers2Map.put(interviewers2List.getFirstName()+" "+interviewers2List.getLastName(), interviewers2List.getEmployeeId());					
				}					
			}
			
			SAPERPModule technology = rrfDAO.getTechnologyByTechnologyName(selectedTechnology);
			primarySkillSouce = rrfDAO.getSkillsByTechnology(technology.getSAPERPModuleId());			
			setPrimarySkills(new DualListModel<String>(primarySkillSouce, primarySkillTarget));  	
		}

	}
	//CLOSE DIALOG BOX
/*  public void dialogClose(ActionEvent e){
	 System.out.println("INSIDE CLOSE METHOD:::::::");
		//Replacing the arraylist content with original record from database...
	
	   rrfRequests.set(rrfRequests.indexOf(slectedRecruitmentRequest), rrfDAO.getRRFByRecruitmentRequestCode(slectedRecruitmentRequest.getRecruitmentRequestCode()));
	
		RequestContext.getCurrentInstance().execute("rrfEditDialog.hide();");		
	}*/
 //UPDATE RRF REQUEST
 public void updateRRFRequest(ActionEvent e){
	 System.out.println("INSIDE UPDATE METHOD:::::");
		
		validate();
		
		if(FORM_VALID){
			System.out.println("Updating Employee...");
			rrfDAO.updateRRFRequest(slectedRecruitmentRequest);	
			
			RequestContext.getCurrentInstance().execute("rrfEditDialog.hide();");
			slectedRecruitmentRequest.setBand(new Band());
			slectedRecruitmentRequest.setInterviewer1(new Employee());
			slectedRecruitmentRequest.setInterviewer2(new Employee());
			slectedRecruitmentRequest.setEmployee(new Employee());
			slectedRecruitmentRequest.setRecruiter(new Employee());
			slectedRecruitmentRequest.setCenterHead(new Employee());
		}else{
			RequestContext.getCurrentInstance().execute("rrfEditDialog.show();");			
		}
	}
 
 public void validate(){
		setFORM_VALID(true);		
		
		if(ValidationUtil.isEmpty(slectedRecruitmentRequest.getBand().toString(), "Band", null, FacesContext.getCurrentInstance()) != null){
			setFORM_VALID(false);
		}
		if(ValidationUtil.isEmpty(slectedRecruitmentRequest.getBand().getBusinessFunction(), "Business Function", null, FacesContext.getCurrentInstance()) != null){
			setFORM_VALID(false);
		}
 }
	//SETTER AND GETTER METHODS
	
	public WAMUser getWamUser() {
		return wamUser;
	}
	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}
	public List<RecruitmentRequest> getRrfRequests() {
		return rrfRequests;
	}

	public void setRrfRequests(List<RecruitmentRequest> rrfRequests) {
		this.rrfRequests = rrfRequests;
	}

	public RecruitmentRequest getSlectedRecruitmentRequest() {
		return slectedRecruitmentRequest;
	}

	public void setSlectedRecruitmentRequest(RecruitmentRequest slectedRecruitmentRequest) {
		this.slectedRecruitmentRequest = slectedRecruitmentRequest;
	}

	public DataTable getRrfTable() {
		return rrfTable;
	}

	public void setRrfTable(DataTable rrfTable) {
		this.rrfTable = rrfTable;
	}

	public Map<String, Long> getInterviewers2Map() {
		return interviewers2Map;
	}

	public void setInterviewers2Map(Map<String, Long> interviewers2Map) {
		this.interviewers2Map = interviewers2Map;
	}

	public Long getSelectedBandId() {
		return selectedBandId;
	}

	public void setSelectedBandId(Long selectedBandId) {
		this.selectedBandId = selectedBandId;
	}

	public String getSelectedBusinessFunction() {
		return selectedBusinessFunction;
	}

	public void setSelectedBusinessFunction(String selectedBusinessFunction) {
		this.selectedBusinessFunction = selectedBusinessFunction;
	}

	public String getSelectedSupportFunction() {
		return selectedSupportFunction;
	}

	public void setSelectedSupportFunction(String selectedSupportFunction) {
		this.selectedSupportFunction = selectedSupportFunction;
	}

	public String getSelectedTechnology() {
		return selectedTechnology;
	}

	public void setSelectedTechnology(String selectedTechnology) {
		this.selectedTechnology = selectedTechnology;
	}

	public Long getSelectedRecruiterId() {
		return selectedRecruiterId;
	}

	public void setSelectedRecruiterId(Long selectedRecruiterId) {
		this.selectedRecruiterId = selectedRecruiterId;
	}
	public List<Band> getBands() {
		return bands;
	}
	public void setBands(List<Band> bands) {
		this.bands = bands;
	}
	public Map<String, Long> getBandsMap() {
		return bandsMap;
	}
	public void setBandsMap(Map<String, Long> bandsMap) {
		this.bandsMap = bandsMap;
	}

	public List<Band> getBusinessFunctions() {
		return businessFunctions;
	}

	public void setBusinessFunctions(List<Band> businessFunctions) {
		this.businessFunctions = businessFunctions;
	}

	public Map<String, Long> getBusinessFunctionMap() {
		return businessFunctionMap;
	}

	public void setBusinessFunctionMap(Map<String, Long> businessFunctionMap) {
		this.businessFunctionMap = businessFunctionMap;
	}

	public List<Band> getSupportFunctions() {
		return supportFunctions;
	}

	public void setSupportFunctions(List<Band> supportFunctions) {
		this.supportFunctions = supportFunctions;
	}

	public Map<String, Long> getSupportFunctionMap() {
		return supportFunctionMap;
	}

	public void setSupportFunctionMap(Map<String, Long> supportFunctionMap) {
		this.supportFunctionMap = supportFunctionMap;
	}

	public Map<String,String> getClientMap() {
		return clientMap;
	}

	public void setClientMap(Map<String,String> clientMap) {
		this.clientMap = clientMap;
	}

	public String getSelectedClient() {
		return selectedClient;
	}

	public void setSelectedClient(String selectedClient) {
		this.selectedClient = selectedClient;
	}

	public String getSelectedInterviewLevel() {
		return selectedInterviewLevel;
	}

	public void setSelectedInterviewLevel(String selectedInterviewLevel) {
		this.selectedInterviewLevel = selectedInterviewLevel;
	}
	public Map<String, Long> getInterviewersMap() {
		return interviewersMap;
	}

	public void setInterviewersMap(Map<String, Long> interviewersMap) {
		this.interviewersMap = interviewersMap;
	}

	public List<Employee> getRecruiters() {
		return recruiters;
	}

	public void setRecruiters(List<Employee> recruiters) {
		this.recruiters = recruiters;
	}

	public List<Technology> getTechnologies() {
		return technologies;
	}

	public void setTechnologies(List<Technology> technologies) {
		this.technologies = technologies;
	}

	public Map<String, Long> getRecruitersMap() {
		return recruitersMap;
	}

	public void setRecruitersMap(Map<String, Long> recruitersMap) {
		this.recruitersMap = recruitersMap;
	}

	public Map<String, String> getTechnologiesMap() {
		return technologiesMap;
	}

	public void setTechnologiesMap(Map<String, String> technologiesMap) {
		this.technologiesMap = technologiesMap;
	}

	public Map<String, String> getInterviewLevelMap() {
		return interviewLevelMap;
	}

	public void setInterviewLevelMap(Map<String, String> interviewLevelMap) {
		this.interviewLevelMap = interviewLevelMap;
	}

	public List<Employee> getInterviewers() {
		return interviewers;
	}

	public void setInterviewers(List<Employee> interviewers) {
		this.interviewers = interviewers;
	}

	public DualListModel<String> getPrimarySkills() {
		return primarySkills;
	}

	public void setPrimarySkills(DualListModel<String> primarySkills) {
		this.primarySkills = primarySkills;
	}

	public List<String> getPrimarySkillSouce() {
		return primarySkillSouce;
	}

	public void setPrimarySkillSouce(List<String> primarySkillSouce) {
		this.primarySkillSouce = primarySkillSouce;
	}

	public List<String> getPrimarySkillTarget() {
		return primarySkillTarget;
	}

	public void setPrimarySkillTarget(List<String> primarySkillTarget) {
		this.primarySkillTarget = primarySkillTarget;
	}

	public Long getSelectedInterviewerOne() {
		return selectedInterviewerOne;
	}

	public void setSelectedInterviewerOne(Long selectedInterviewerOne) {
		this.selectedInterviewerOne = selectedInterviewerOne;
	}

	public Long getSelectedInterviewerTwo() {
		return selectedInterviewerTwo;
	}

	public void setSelectedInterviewerTwo(Long selectedInterviewerTwo) {
		this.selectedInterviewerTwo = selectedInterviewerTwo;
	}

	public Map<String,String> getRecruitmentModelMap() {
		return recruitmentModelMap;
	}

	public void setRecruitmentModelMap(Map<String,String> recruitmentModelMap) {
		this.recruitmentModelMap = recruitmentModelMap;
	}

	public Map<String,String> getRecruitmentPriorityMap() {
		return recruitmentPriorityMap;
	}

	public void setRecruitmentPriorityMap(Map<String,String> recruitmentPriorityMap) {
		this.recruitmentPriorityMap = recruitmentPriorityMap;
	}

	public String getSelectedRecruitmentMode() {
		return selectedRecruitmentMode;
	}

	public void setSelectedRecruitmentMode(String selectedRecruitmentMode) {
		this.selectedRecruitmentMode = selectedRecruitmentMode;
	}

	public String getSelectedRecruitmentPriority() {
		return selectedRecruitmentPriority;
	}

	public void setSelectedRecruitmentPriority(
			String selectedRecruitmentPriority) {
		this.selectedRecruitmentPriority = selectedRecruitmentPriority;
	}
	public String getRecruitmentRequestCode() {
		return recruitmentRequestCode;
	}

	public void setRecruitmentRequestCode(String recruitmentRequestCode) {
		this.recruitmentRequestCode = recruitmentRequestCode;
	}
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}
}
